package cn.edu.neusoft.yuejian13.yuejian_13.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import cn.edu.neusoft.yuejian13.yuejian_13.R;
import cn.edu.neusoft.yuejian13.yuejian_13.fragment.ClubFragment;
import cn.edu.neusoft.yuejian13.yuejian_13.fragment.HomeFragment;
import cn.edu.neusoft.yuejian13.yuejian_13.fragment.MyFragment;
import cn.edu.neusoft.yuejian13.yuejian_13.fragment.ShowFragment;
import cn.edu.neusoft.yuejian13.yuejian_13.fragment.TrainFragment;




public class MainActivity extends FragmentActivity implements View.OnClickListener{

    private LinearLayout mTabTrain;
    private LinearLayout mTabClub;
    private LinearLayout mTabHome;
    private LinearLayout mTabShow;
    private LinearLayout mTabMy;

    private ImageButton mImgTrain;
    private ImageButton mImgCLub;
    private ImageButton mImgHome;
    private ImageButton mImgShow;
    private ImageButton mImgMy;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(1);

    }
    private void initEvent()
    {
        mTabTrain.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
        mTabHome.setOnClickListener(this);
        mTabShow.setOnClickListener(this);
        mTabMy.setOnClickListener(this);

    }

    private void initView()
    {
        mTabTrain = (LinearLayout) findViewById(R.id.id_tab_train);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabHome = (LinearLayout) findViewById(R.id.id_tab_home);
        mTabShow = (LinearLayout) findViewById(R.id.id_tab_show);
        mTabMy = (LinearLayout) findViewById(R.id.id_tab_my);


        mImgTrain = (ImageButton) findViewById(R.id.id_tab_My_img);
        mImgCLub = (ImageButton) findViewById(R.id.id_tab_Club_img);
        mImgHome = (ImageButton) findViewById(R.id.id_tab_Home_img);
        mImgShow = (ImageButton) findViewById(R.id.id_tab_Show_img);
        mImgMy  = (ImageButton) findViewById(R.id.id_tab_My_img);

    }
    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 1:
                if (mTab01 == null) {
                    mTab01 = new TrainFragment();
                   transaction.add(R.id.id_content,mTab01);
                } else {
                    transaction.show(mTab01);
                }
               // mImgHome.setImageResource(R.drawable.ic_home1);
                break;
            case 2:
                if(mTab02 == null){
                    mTab02 = new ClubFragment();
                    transaction.add(R.id.id_content,mTab02);
                }else{
                    transaction.show(mTab02);
                }
                break;
            case 3:
                if(mTab03 == null){
                    mTab03 = new HomeFragment();
                    transaction.add(R.id.id_content,mTab03);
                }else{
                    transaction.show(mTab03);
                }
                break;
            case 4:
                if(mTab04 == null){
                    mTab04 = new ShowFragment();
                    transaction.add(R.id.id_content,mTab04);
                }else{
                    transaction.show(mTab04);
                }
                break;
            case 5:
                if(mTab04 == null){
                    mTab05 = new MyFragment();
                    transaction.add(R.id.id_content,mTab05);
                }else{
                    transaction.show(mTab05);
                }
                break;

            default:
                break;
        }

        transaction.commit();
    }


    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.id_tab_train:
                setSelect(1);
                break;
            case R.id.id_tab_club:
                setSelect(2);
                break;
            case R.id.id_tab_home:
                setSelect(3);
                break;
            case R.id.id_tab_show:
                setSelect(4);
                break;
            case R.id.id_tab_my:
                setSelect(5);
                break;

            default:
                break;
        }


    }
}
